using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace de_kleintjesmarkt
{
    public partial class CtlLogin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // (1) Check if browser accepts cookies.
            if (Request.Browser.Cookies)
            {
                // (2) If the cookie does not exist...
                if (Request.Cookies["LastVisit"] == null)
                {
                    // (3) Create the cookie.
                    HttpCookie cookLastVisit = new HttpCookie("LastVisit",
                        DateTime.Now.ToString());
                    // (4) Set the expiration to next year.
                    cookLastVisit.Expires = DateTime.Now.AddDays(365);
                    // (5) Add to cookies collection.
                    Response.Cookies.Add(cookLastVisit);
                }
                // (2) If the cookie does exist...
                else
                {
                    // (3) Get the cookie.
                    HttpCookie cookLastVisit = Request.Cookies["LastVisit"];
                    // (4) Display a message showing time of last visit.
                    litMessage.Text = "Laatste bezoek: <br />" +
                        cookLastVisit.Value;
                    // (5) Update the cookie on the client.
                    Response.Cookies["LastVisit"].Value =
                        DateTime.Now.ToString();
                    // (6) Set the expiration date to next year.
                    Response.Cookies["LastVisit"].Expires =
                        DateTime.Now.AddDays(365);
                }
            }
            else
            {
                litMessage.Text = "Uw browser ondersteunt geen cookies.";
            }
        }

        protected void butRegistreren_Click(object sender, EventArgs e)
        {
            Response.Redirect("Registreren.aspx");
        }

        protected void chkbxOnthouden_CheckedChanged(object sender, EventArgs e)
        {
            // Remember Login
        }

        protected void butSignOn_Click(object sender, EventArgs e)
        {
          // If the password has not been hashed javascript is disabled.
          // We'll hash it server-side.
          if("0" == hfIsHashed.Value)
          {
              txtWachtwoord.Text = ChallengeBuilder.createMD5(txtEmail.Text + txtWachtwoord.Text);
          }
            
          // Login using the sp_validateLogin stored procedure.
          LoginService.DbService loginService = new LoginService.DbService();
          
          bool result = loginService.ValidateLogin(txtEmail.Text, txtWachtwoord.Text);
          if (result == false)
             litMessage.Text = "Verkeerde combinatie van e-mail en wachtwoord.";
          else if (result == true)
          {
              //Cant store the klant object cause it will be used in the different services. 
              //KlantInfo.KlantenInfo klantinfo = new KlantInfo.KlantenInfo();
              //KlantInfo.Klant klant = klantinfo.GetKlantInfo(txtEmail.Text);
             Session ["klant"] = txtEmail.Text;
             Response.Redirect("PlaatsAdvertentie.aspx");
          }
        }
    }
}